package com.tanhua.dubbo.api;

import com.tanhua.model.enums.VideoType;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.mongo.VideoShow;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class VideoShowApiImpl implements VideoShowApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<VideoShow> findVideos(String videoId, VideoType videoType, Integer page, Integer pagesize) {
        //1、构造查询条件
        Query query = Query.query(Criteria.where("videoId").is(new ObjectId(videoId)).and("videoType")
                .is(videoType.getType()))
                .skip((page - 1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        //2、查询并返回
        return mongoTemplate.find(query, VideoShow.class);
    }

    // 判断是否是否对视频点赞
    @Override
    public Boolean hasComment(String videoId, Long userId, VideoType like) {
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("videoId").is(new ObjectId(videoId))
                .and("videoType").is(like.getType());
        Query query = Query.query(criteria);
        return mongoTemplate.exists(query, VideoShow.class); //判断数据是否存在
    }

    @Override
    public Integer delete(VideoShow videoShow) {
        //1、删除Comment表数据
        Criteria criteria = Criteria.where("userId").is(videoShow.getUserId())
                .and("videoId").is(videoShow.getVideoId())
                .and("videoType").is(videoShow.getVideoType());
        Query query = Query.query(criteria);
        mongoTemplate.remove(query, VideoShow.class);
        //2、修改动态表中的总数量
        Query videoQuery = Query.query(Criteria.where("id").is(videoShow.getVideoId()));
        Update update = new Update();
        if (videoShow.getVideoType() == VideoType.LIKE.getType()) {
            update.inc("likeCount", -1);
        } else if (videoShow.getVideoType() == VideoType.COMMENT.getType()) {
            update.inc("commentCount", -1);
        } else {
            update.inc("loveCount", -1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);//获取更新后的最新数据
        Video modify = mongoTemplate.findAndModify(videoQuery, update, options, Video.class);
        //5、获取最新的评论数量，并返回
        return modify.statisCount(videoShow.getVideoType());

    }

    // 判断用户是否对评论
    @Override
    public Boolean hasVideoShow(String videoShowId, Long userId, VideoType like) {
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("videoId").is(new ObjectId(videoShowId))
                .and("videoType").is(like.getType());
        Query query = Query.query(criteria);
        return mongoTemplate.exists(query, VideoShow.class); //判断数据是否存在
    }

    // 根据id查询
    @Override
    public VideoShow findVideo(String commentId, Long userId) {
        Query query = Query.query(Criteria.where("videoId").is(new ObjectId(commentId)).and("userId").is(userId));
        return mongoTemplate.findOne(query, VideoShow.class);
    }

//    // 根据id查询
//    @Override
//    public VideoShow findVideo(String commentId, Integer videoType) {
//        Query query = Query.query(Criteria.where("videoId").is(commentId).and("videoType").is(videoType));
//        return mongoTemplate.findOne(query, VideoShow.class);
//    }

    // 保存视频点赞详情`
    @Override
    public void saveComment(VideoShow videoShow) {
        //1、查询动态
        Video video = mongoTemplate.findById(videoShow.getVideoId(), Video.class);
        //2、向video对象设置被评论人属性
        if (video != null) {
            videoShow.setVideoUserId(video.getUserId());
        }
        //3、保存到数据库
        mongoTemplate.save(videoShow);
    }

    // 删除视频评论点赞
    @Override
    public void deleteVideoShow(VideoShow videoShow) {
        mongoTemplate.remove(videoShow);
    }

    @Override
    public Boolean findVideoShow(String videoId, Long userId, Integer isLike) {
        Query query = Query.query(Criteria.where("videoId").is(new ObjectId(videoId)).and("userId").is(userId)
        .and("videoType").is(isLike));
        return mongoTemplate.exists(query, VideoShow.class);
    }

    // 保存对视频评论点赞
    @Override
    public Integer saveViodeLike(VideoShow videoShow) {
        //1、查询动态
        Video video = mongoTemplate.findById(videoShow.getVideoId(), Video.class);
        //2、向video对象设置被评论人属性
        if (video != null) {
            videoShow.setVideoUserId(video.getUserId());
        }
        //3、保存到数据库
        mongoTemplate.save(videoShow);
        //4、更新动态表中的对应字段
        Query query = Query.query(Criteria.where("_id").is(videoShow.getVideoId()));
        Update update = new Update();
        if (videoShow.getVideoType() == VideoType.LIKE.getType()) {
            update.inc("likeCount", 1);
        }else if (videoShow.getVideoType() == VideoType.COMMENT.getType()) {
            update.inc("commentCount", 1);
        } else {
            update.inc("loveCount", 1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);//获取更新后的最新数据
        Video modify = mongoTemplate.findAndModify(query, update, options, Video.class);
        //5、获取最新的评论数量，并返回
        return modify.statisCount(videoShow.getVideoType());
    }


}
